package org.codeCard.panel;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.JpanelOnJalbelBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;
import come.tool.JDialog.TiShiUtil;
import org.apache.commons.lang.StringUtils;
import org.cbg.bean.SearchGoodsBean;
import org.cbg.btn.TrslationBtn;
import org.codeCard.bean.TbSaleCardOrder;
import org.codeCard.btn.CodeCardBtn;
import org.codeCard.btn.DianKaBuyBtn;
import org.codeCard.frame.CodeCardJFrame;
import org.codeCard.model.CodeCardSellerModel;
import org.codeCard.util.CodeCardUntil;
import org.come.Frame.OptionsJframe;
import org.come.Frame.ZhuFrame;
import org.come.bean.LoginResult;
import org.come.entity.Goodstable;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;
import org.come.until.Util;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;


public class CodeCardSaleJPanel extends JPanel {


    private static JTextField findName,findName1;

    private CodeCardBtn bjczBtn1, bjczBtn2;

    private Goodstable good;
    private static String[] Fvalue;
    private static JTextArea testMes;// 用来显示聊天信息

    private TrslationBtn shouye, moye, leftArrows, rightArrows, chooseLeftArrows, chooseRightArrows;

    private JScrollPane jScrollPane;

    private JTextField jiShouDianShu;

    private JTextField zongJia;

    private String meiDian;

    private String shouXuFei;

    private String woDeXianYu;

    private String xianJin;

    private DianKaBuyBtn sellerBtn, xiajia;

    private JLabel chooseLeft, chooseRight, yema, upSort, downSort;
    /**
     * 当前页码 最大页码
     *
     */
    private int dangqianyeshu = 1, zuidayema = 50;

    private CodeCardSellerModel sellerXianYuModelJpanel;

    BigDecimal middlePay = new BigDecimal("0.05");

    public TbSaleCardOrder customerOrder;


    public CodeCardSaleJPanel() {

        yema = new JLabel(dangqianyeshu + "/" + zuidayema);
        jScrollPane = new JScrollPane();
        this.zongJia = new JTextField();

        if (MyIsif.getStyle().equals("水墨UI")) {


            this.zongJia.setBounds(195,380, 88, 23);

            jScrollPane.setBounds(10, 120, 390, 180);

            this.jiShouDianShu = new JTextField();
            this.jiShouDianShu.setBounds(195,348, 88, 23);


            xiajia = new DianKaBuyBtn("skin/8100226/newbutton/B32.png", 1, UIUtils.COLOR_BTNTEXT, 11, "下架", this);
            xiajia.setBounds(342, 306, 58, 30);

            sellerBtn = new DianKaBuyBtn("inkImg/number/b9/水墨寄售.png", 1, UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY88, "", this);
            sellerBtn.setBounds(208, 449, 99, 30);


            leftArrows = new TrslationBtn("inkImg/number/b9/水墨上一页.png", 1);
            leftArrows.setBounds(176, 310, 50, 20);

            rightArrows = new TrslationBtn("inkImg/number/b9/水墨下一页.png", 1);
            rightArrows.setBounds(286, 310, 50, 20);

            yema.setBounds(228, 312, 58, 17);
            jScrollPane.setBounds(60, 100, 390, 180);

            this.setPreferredSize(new Dimension(481,500));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("skin/8100226/button/1.png", 1, 690);
            offBtn.setBounds(444, 10, 25, 25);
            this.add(offBtn);

        }else {

            this.zongJia.setBounds(171,393, 88, 23);

            jScrollPane.setBounds(36, 120, 390, 180);
            this.jiShouDianShu = new JTextField();
            this.jiShouDianShu.setBounds(171,365, 88, 23);

            xiajia = new DianKaBuyBtn("skin/8100226/newbutton/B32.png", 1, UIUtils.COLOR_BTNTEXT, 11, "下架", this);
            xiajia.setBounds(342, 354-35, 58, 30);

            sellerBtn = new DianKaBuyBtn("inkImg/number/b9/红木我寄售.png", 1, UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY88, "", this);
            sellerBtn.setBounds(177, 461, 99, 30);


            leftArrows = new TrslationBtn("inkImg/number/b9/红木上一页.png", 1);
            leftArrows.setBounds(150, 325, 50, 20);

            rightArrows = new TrslationBtn("inkImg/number/b9/红木下一页.png", 1);
            rightArrows.setBounds(256, 325, 50, 20);

            yema.setBounds(195, 327, 58, 17);

            this.setPreferredSize(new Dimension(452,527));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/1/s74.png", 1, 690);
            offBtn.setBounds(428, 0, 25, 25);
            this.add(offBtn);
        }

        CodeCardUntil.TableModel(jScrollPane, 3);
        jScrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
        jScrollPane.getViewport().setBorder(null);
        jScrollPane.setViewportBorder(null);
        jScrollPane.setBorder(null);
        jScrollPane.getVerticalScrollBar().setUnitIncrement(10);
        this.add(jScrollPane);


        this.jiShouDianShu.setOpaque(false);
        this.jiShouDianShu.setBorder(BorderFactory.createEmptyBorder());
        this.jiShouDianShu.setForeground(Color.white);
        this.jiShouDianShu.setFont(new Font("楷体", 0, 15));
        this.jiShouDianShu.setCaretColor(Color.white);
        jiShouDianShu.addKeyListener(new KeyListener() {
            // 键盘监听
            @Override
            public void keyTyped(KeyEvent e) {
                // 让该输入框只能输入数字
                int keyChar = e.getKeyChar();
                if (!(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9)) {
                    e.consume();
                    return;
                }

                if (StringUtils.isNotEmpty(getZongJia().getText()) && StringUtils.isNotEmpty(getJiShouDianShu().getText())) {

                    String key = jiShouDianShu.getText();

                    if(key.indexOf("0") == 0){
                        e.consume();
                        return;
                    }
                    Long value = 9999999999L; // 该框只能输入这个物品的最大数量
                    long l = Long.parseLong(key);
                    if (value < l || l <= 0){
                        e.consume();
                        return;
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
            }
        });
        this.jiShouDianShu.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
                String s = zongJia.getText();
                String text = jiShouDianShu.getText();
                try {
                    if(s!=null && s.length() > 12){
                        return;
                    }
                    if(text!=null && text.length() > 12){
                        return;
                    }

                    if(jiShouDianShu.getText() != null && !jiShouDianShu.getText().equals("0") && !s.equals("0")){
                        BigDecimal bigDecimal = new BigDecimal(s);
                        setMeiDian(bigDecimal.multiply(new BigDecimal(jiShouDianShu.getText())).toPlainString());
                        Util.changeTextColor(zongJia, bigDecimal);
                    }

                    if(!s.equals("0")){
                        BigDecimal bigDecimal = new BigDecimal(getMeiDian());
                        if(bigDecimal.longValue() > 0){
                            setShouXuFei(bigDecimal.multiply(middlePay).setScale(0,BigDecimal.ROUND_HALF_UP).toPlainString());

                        }
                    }else{
                        setShouXuFei("");
                        setMeiDian("");
                    }

                } catch (NumberFormatException e1) {
                }
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
                String s = zongJia.getText();
                String text = jiShouDianShu.getText();
                try {
                    if(s!=null && s.length() > 12){
                        return;
                    }
                    if(text!=null && text.length() > 12){
                        return;
                    }
                    if(jiShouDianShu.getText() != null && !jiShouDianShu.getText().equals("0") && !s.equals("0")){
                        BigDecimal bigDecimal = new BigDecimal(s);
                        BigDecimal divide = bigDecimal.multiply(new BigDecimal(jiShouDianShu.getText()));
                        if(divide != null){
                            setMeiDian(divide.toPlainString());
                        }
                        Util.changeTextColor(zongJia, bigDecimal);
                    }

                    if(!s.equals("0")){
                        BigDecimal bigDecimal = new BigDecimal(getMeiDian());
                        if(bigDecimal.longValue() > 0){
                            setShouXuFei(bigDecimal.multiply(middlePay).setScale(0,BigDecimal.ROUND_HALF_UP).toPlainString());

                        }

                    }else{
                        setShouXuFei("");
                        setMeiDian("");
                    }
                } catch (NumberFormatException e1) {
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
            }
        });

        add(this.jiShouDianShu);


        xiajia.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                if(customerOrder == null || customerOrder.getSalePrice() == null){
                    ZhuFrame.getZhuJpanel().addPrompt2("请先选中要下架的");
                    return;
                }

                OptionsJframe.getOptionsJframe().getOptionsJpanel().showBox(TiShiUtil.UNDER_SELLER_CARD, customerOrder,
                        "#Y你确定要下架 #G" + customerOrder.getSalePrice() + " #Y 在售的 #R"+ customerOrder.getCardCode()+" #Y点仙玉?");
            }
        });

        this.add(xiajia);

        sellerBtn.addKeyListener(new KeyAdapter(){
            public void keyTyped(KeyEvent e) {
                int keyChar = e.getKeyChar();
                if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){

                }else{
                    e.consume(); //关键，屏蔽掉非法输入
                }
            }
        });


        sellerBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                if (!Util.canBuyOrno) {
                    ZhuFrame.getZhuJpanel().addPrompt2("背包没有解锁!");
                    FormsManagement.showForm(33);
                    return;
                }

                // TODO Auto-generated method stub
                final String jiShouDianShuText = jiShouDianShu.getText();
                if(StringUtils.isEmpty(jiShouDianShuText)){
                    ZhuFrame.getZhuJpanel().addPrompt2("寄售金额为空");
                    return;
                }

                final String zongJiaText = zongJia.getText();
                if(StringUtils.isEmpty(zongJiaText)){
                    ZhuFrame.getZhuJpanel().addPrompt2("金额为空");
                    return;
                }
                TbSaleCardOrder saleCardOrder = new TbSaleCardOrder();
                saleCardOrder.setCardCode(new BigDecimal(jiShouDianShu.getText()));
                saleCardOrder.setSalePrice(new BigDecimal(getMeiDian()));

                OptionsJframe.getOptionsJframe().getOptionsJpanel().showBox(TiShiUtil.CONFIRM_SELL_CARD, saleCardOrder,
                        "#Y你确定要以 #G" + saleCardOrder.getSalePrice()+ " #Y来售卖 #R"+saleCardOrder.getCardCode()+" #Y点仙玉?");

            }
        });

        this.add(leftArrows);
        leftArrows.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (dangqianyeshu > 1) {
                    dangqianyeshu--;
                    yema.setText(dangqianyeshu + "/" + zuidayema);

                    SearchGoodsBean searchGoodsBean1 = new SearchGoodsBean();
                    searchGoodsBean1.setSaletype(2);
                    searchGoodsBean1.setPageNum(dangqianyeshu);
                    String sendmes1 = Agreement.getAgreement().saleCardMySearchAgreement(
                            GsonUtil.getGsonUtil().getgson().toJson(searchGoodsBean1));
                    SendMessageUntil.toServer(sendmes1);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("已经没有上一页了");
                }
            }
        });

        this.add(rightArrows);
        rightArrows.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (dangqianyeshu < zuidayema) {
                    dangqianyeshu++;
                    yema.setText(dangqianyeshu + "/" + zuidayema);

                    SearchGoodsBean searchGoodsBean1 = new SearchGoodsBean();
                    searchGoodsBean1.setSaletype(2);
                    searchGoodsBean1.setPageNum(dangqianyeshu);
                    String sendmes1 = Agreement.getAgreement().saleCardMySearchAgreement(
                            GsonUtil.getGsonUtil().getgson().toJson(searchGoodsBean1));
                    SendMessageUntil.toServer(sendmes1);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("已经没有下一页了");
                }
            }
        });


        this.zongJia.setOpaque(false);
        this.zongJia.setBorder(BorderFactory.createEmptyBorder());
        this.zongJia.setForeground(Color.white);
        this.zongJia.setFont(new Font("楷体", 0, 15));
        this.zongJia.setCaretColor(Color.white);
        zongJia.addKeyListener(new KeyListener() {
            // 键盘监听
            @Override
            public void keyTyped(KeyEvent e) {
                // 让该输入框只能输入数字
                int keyChar = e.getKeyChar();
                if (!(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9)) {
                    e.consume();
                    return;
                }

                if (StringUtils.isNotEmpty(getZongJia().getText()) && StringUtils.isNotEmpty(getJiShouDianShu().getText())) {
                    String key = zongJia.getText();

                    if(key.indexOf("0") == 0){
                        e.consume();
                        return;
                    }
                    Long value = 999999999999L; // 该框只能输入这个物品的最大数量
                    long l = Long.parseLong(key);
                    if (value < l || l <= 0){
                        e.consume();
                        return;
                    }

                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
            }
        });
        this.zongJia.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
                String s = zongJia.getText();
                String text = jiShouDianShu.getText();
                try {
                    if(s!=null && s.length() > 12){
                        return;
                    }
                    if(text!=null && text.length() > 12){
                        return;
                    }
                    if(jiShouDianShu.getText() != null && !jiShouDianShu.getText().equals("0") && !s.equals("0")){
                        BigDecimal bigDecimal = new BigDecimal(s);
                        setMeiDian(bigDecimal.multiply(new BigDecimal(jiShouDianShu.getText())).toPlainString());
                        Util.changeTextColor(zongJia, bigDecimal);
                    }

                    if(!s.equals("0")){
                        BigDecimal bigDecimal = new BigDecimal(getMeiDian());
                        if(bigDecimal.longValue() > 0){
                            setShouXuFei(bigDecimal.multiply(middlePay).setScale(0,BigDecimal.ROUND_HALF_UP).toPlainString());

                        }
                    }else{
                        setShouXuFei("");
                        setMeiDian("");
                    }

                } catch (NumberFormatException e1) {
                }
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
                String s = zongJia.getText();
                String text = jiShouDianShu.getText();
                try {
                    if(s!=null && s.length() > 12){
                        return;
                    }
                    if(text!=null && text.length() > 12){
                        return;
                    }
                    if(jiShouDianShu.getText() != null && !jiShouDianShu.getText().equals("0") && !s.equals("0")){
                        BigDecimal bigDecimal = new BigDecimal(s);
                        BigDecimal divide = bigDecimal.multiply(new BigDecimal(jiShouDianShu.getText()));
                        if(divide != null){
                            setMeiDian(divide.toPlainString());
                        }
                        Util.changeTextColor(zongJia, bigDecimal);
                    }

                    if(!s.equals("0")){
                        BigDecimal bigDecimal = new BigDecimal(getMeiDian());
                        if(bigDecimal.longValue() > 0){
                            setShouXuFei(bigDecimal.multiply(middlePay).setScale(0,BigDecimal.ROUND_HALF_UP).toPlainString());

                        }

                    }else{
                        setShouXuFei("");
                        setMeiDian("");
                    }
                } catch (NumberFormatException e1) {
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
            }


        });

        add(this.zongJia);

        this.add(sellerBtn);

        yema.setForeground(Color.white);
        yema.setOpaque(false);
        yema.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(yema);

        getBtnTeam();
        getBtnSkill();
    }


    private ImageIcon smIcon = CutButtonImage.getImage("inkImg/number/b9/b9-sm-sell-bg.png", 481,500);
    private ImageIcon hmIcon = CutButtonImage.getImage("inkImg/number/b9/b9-hm-sell-bg.png", 452,527);
    LoginResult login;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);


        if (MyIsif.getStyle().equals("水墨UI")) {
            g.drawImage(smIcon.getImage(), 0, 0, 481,500, this);

            if(meiDian != null){
                meiDian = meiDian.equals("") ? "0": meiDian;
                Util.drawPrice(g, new BigDecimal(meiDian), 336,365);
            }

            if(shouXuFei != null){
                shouXuFei = shouXuFei.equals("") ? "0": shouXuFei;
                Util.drawPrice(g, new BigDecimal(shouXuFei), 336, 392);
            }
            login = RoleData.getRoleData().getLoginResult();

            Util.drawPrice(g, login.getCodecard(), 195, 421);

            Util.drawPrice(g, login.getGold(), 338, 421);
        }else {
            g.drawImage(hmIcon.getImage(), 0, 0, 452,527, this);

            if(meiDian != null){
                meiDian = meiDian.equals("") ? "0": meiDian;
                Util.drawPrice(g, new BigDecimal(meiDian), 308,379);
            }

            if(shouXuFei != null){
                shouXuFei = shouXuFei.equals("") ? "0": shouXuFei;
                Util.drawPrice(g, new BigDecimal(shouXuFei), 308, 408);
            }
            login = RoleData.getRoleData().getLoginResult();

            Util.drawPrice(g, login.getCodecard(), 171, 438);

            Util.drawPrice(g, login.getGold(), 308, 438);
        }

    }

    public CodeCardBtn getBtnTeam() {
        if(this.bjczBtn1 != null){
            this.remove(bjczBtn1);
            bjczBtn1 = null;
        }
        if (MyIsif.getStyle().equals("水墨UI")) {

            bjczBtn1 = new CodeCardBtn("inkImg/number/b9/b9-sm-sell-big.png", 1, 2, CodeCardJFrame.bjczMainJpanel);
            bjczBtn1.setBounds(160, 18, 85, 35);
            this.add(bjczBtn1);

        }else {

            bjczBtn1 = new CodeCardBtn("inkImg/number/b9/b9-hm-sell-big.png", 1, 2, CodeCardJFrame.bjczMainJpanel);
            bjczBtn1.setBounds(128, 29, 85, 35);
            this.add(bjczBtn1);

        }
        return bjczBtn1;
    }


    public CodeCardBtn getBtnSkill() {
        if(this.bjczBtn2 != null){
            this.remove(bjczBtn2);
            bjczBtn2 = null;
        }
        if (MyIsif.getStyle().equals("水墨UI")) {

            bjczBtn2 = new CodeCardBtn("inkImg/number/b9/b9-sm-buy-small.png", 1, 1, CodeCardJFrame.bjczMainJpanel);
            bjczBtn2.setBounds(66, 19, 85, 35);
            this.add(bjczBtn2);

        }else {

            bjczBtn2 = new CodeCardBtn("inkImg/number/b9/b9-hm-buy-small.png", 1, 1, CodeCardJFrame.bjczMainJpanel);
            bjczBtn2.setBounds(38, 32, 85, 35);
            this.add(bjczBtn2);

        }
        return bjczBtn2;
    }

    public static JTextField getFindName() {
        return findName;
    }

    public static void setFindName(JTextField findName) {
        CodeCardSaleJPanel.findName = findName;
    }

    public static JTextField getFindName1() {
        return findName1;
    }

    public static void setFindName1(JTextField findName1) {
        CodeCardSaleJPanel.findName1 = findName1;
    }

    public Goodstable getGood() {
        return good;
    }

    public void setGood(Goodstable good) {
        this.good = good;
    }

    public String[] getFvalue() {
        return Fvalue;
    }

    public void setFvalue(String[] fvalue) {
        Fvalue = fvalue;
    }

    public JTextArea getTestMes() {
        return testMes;
    }

    public void setTestMes(JTextArea testMes) {
        this.testMes = testMes;
    }

    public JTextField getJiShouDianShu() {
        return jiShouDianShu;
    }

    public void setJiShouDianShu(JTextField jiShouDianShu) {
        this.jiShouDianShu = jiShouDianShu;
    }

    public JTextField getZongJia() {
        return zongJia;
    }

    public void setZongJia(JTextField zongJia) {
        this.zongJia = zongJia;
    }

    public String getMeiDian() {
        return meiDian;
    }

    public void setMeiDian(String meiDian) {
        this.meiDian = meiDian;
    }

    public String getShouXuFei() {
        return shouXuFei;
    }

    public void setShouXuFei(String shouXuFei) {
        this.shouXuFei = shouXuFei;
    }

    public TbSaleCardOrder getCustomerOrder() {
        return customerOrder;
    }

    public void setCustomerOrder(TbSaleCardOrder customerOrder) {
        this.customerOrder = customerOrder;
    }

    public void setPage(int zuidayema) {
        this.zuidayema = zuidayema;
        yema.setText(this.dangqianyeshu + "/" + this.zuidayema);
    }

    public JScrollPane getjScrollPane() {
        return jScrollPane;
    }

    public void setjScrollPane(JScrollPane jScrollPane) {
        this.jScrollPane = jScrollPane;
    }
}

